﻿using Common.Utils;
using System;
using Windows.UI.Xaml.Data;

namespace Common.UI.Converters
{
	public class DateTimeDeltaConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, string language)
		{
			if (value == null)
				return null;

			var date = (DateTime)value;
			var delta = DateTimeHelper.Now - date;

			

			if (delta.TotalMinutes < 1)
				return "a moment ago";
			if (delta.TotalHours < 1)
				return String.Format("{0} minutes ago", Math.Ceiling(delta.TotalMinutes));
			if (delta.TotalDays < 1)
				return String.Format("{0} hours ago", Math.Ceiling(delta.TotalHours));

			if (delta.TotalDays < 7)
				return String.Format("{0} days ago", Math.Ceiling(delta.TotalDays));
		
			var weeks = delta.TotalDays / 7;
			if (weeks < 6)
				return String.Format("{0} weeks ago", weeks);

			return "long ago";
		}

		public object ConvertBack(object value, Type targetType, object parameter, string language)
		{
			throw new NotImplementedException();
		}
	}
}
